/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.form.client.oracle;

import java.util.List;

import com.genia.toolbox.web.gwt.basics.client.GwtHelper;
import com.genia.toolbox.web.gwt.basics.client.i18n.GwtI18nMessage;
import com.genia.toolbox.web.gwt.basics.client.oracle.AbstractCustomSuggestOracle;
import com.genia.toolbox.web.gwt.basics.client.oracle.SimpleSuggestion;
import com.genia.toolbox.web.gwt.form.client.message.Messages;
import com.genia.toolbox.web.gwt.form.client.process.ProcessForm;
import com.genia.toolbox.web.gwt.form.client.value.DataIdentifier;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasText;

/**
 * a <code>SuggestOracle</code> that calls a FormService to get the result.
 */
public class FormServiceSuggestOracle
    extends AbstractCustomSuggestOracle
{

  /**
   * the identifier of the form using this {@link AbstractCustomSuggestOracle}.
   */
  private String formIdentifier;

  /**
   * the data identifier.
   */
  private DataIdentifier dataIdentifier;

  /**
   * the name of the field that must be filled.
   */
  private String fieldName;

  /**
   * wether to display html.
   */
  private boolean displayStringHTML;



  /**
   * getter for the fieldName property.
   * 
   * @return the fieldName
   */
  public String getFieldName()
  {
    return fieldName;
  }



  /**
   * getter for the formIdentifier property.
   * 
   * @return the formIdentifier
   */
  public String getFormIdentifier()
  {
    return formIdentifier;
  }



  /**
   * Generate a {@link Response} based on a specific {@link Request}. After the
   * {@link Response} is created, it is passed into {@link Callback}).
   * 
   * @param request
   *          the request
   * @param callback
   *          the callback to use for the response
   */
  @Override
  public void requestSuggestions(final Request request, final Callback callback)
  {
    ProcessForm.getFormService().getSuggestions(getFormIdentifier(), getDataIdentifier(), getFieldName(), request, new AsyncCallback<List<SimpleSuggestion>>()
    {

      public void onFailure(final Throwable cause)
      {
        GwtHelper.error(new GwtI18nMessage(Messages.UNABLE_TO_RETRIEVE_SUGGESTIONS_ERROR_KEY, new String[] { cause.getMessage() }), cause);
      }



      public void onSuccess(final List<SimpleSuggestion> suggestions)
      {
        callback.onSuggestionsReady(request, new Response(suggestions));
      }

    });
  }



  /**
   * request the representation of a given value.
   * 
   * @param value
   *          the value to represent
   * @param hasText
   *          the widget to fill with the representation
   */
  @Override
  public void requestText(final String value, final HasText hasText)
  {
    ProcessForm.getFormService().getDisplayString(getFormIdentifier(), getFieldName(), value, new AsyncCallback<String>()
    {

      public void onFailure(final Throwable cause)
      {
        GwtHelper.error(new GwtI18nMessage(Messages.UNABLE_TO_RETRIEVE_DISPLAY_TEXT_ERROR_KEY, new String[] { cause.getMessage() }), cause);
      }



      public void onSuccess(final String result)
      {
        hasText.setText(result);
      }
    });
  }



  /**
   * setter for the fieldName property.
   * 
   * @param fieldName
   *          the fieldName to set
   */
  public void setFieldName(final String fieldName)
  {
    this.fieldName = fieldName;
  }



  /**
   * setter for the formIdentifier property.
   * 
   * @param formIdentifier
   *          the formIdentifier to set
   */
  public void setFormIdentifier(final String formIdentifier)
  {
    this.formIdentifier = formIdentifier;
  }



  /**
   * getter for the displayStringHTML property.
   * 
   * @return the displayStringHTML
   */
  @Override
  public boolean isDisplayStringHTML()
  {
    return displayStringHTML;
  }



  /**
   * setter for the displayStringHTML property.
   * 
   * @param displayStringHTML
   *          the displayStringHTML to set
   */
  public void setDisplayStringHTML(boolean displayStringHTML)
  {
    this.displayStringHTML = displayStringHTML;
  }



  /**
   * getter for the dataIdentifier property.
   * 
   * @return the dataIdentifier
   */
  public DataIdentifier getDataIdentifier()
  {
    return dataIdentifier;
  }



  /**
   * setter for the dataIdentifier property.
   * 
   * @param dataIdentifier
   *          the dataIdentifier to set
   */
  public void setDataIdentifier(DataIdentifier dataIdentifier)
  {
    this.dataIdentifier = dataIdentifier;
  }

}
